package Ep01_Array.Mar02nd_0977;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/2 14:52
 * 说明：
 */

public class Mine {
    public static void main(String[] args) {
        Mine m = new Mine();
        int[] arr = {-4,-1,0,3,10};
        int[] arr3 = m.sortedSquares(arr);
        for (int i = 0; i < arr3.length; i++) {
            System.out.println(arr3[i]);
        }

    }
    public int[] sortedSquares(int[] nums){
        int p = nums.length - 1;
        int i = 0;
        int counter = 1;
        int[] arr2 = new int[nums.length];
        while (p >= i){
            if (nums[i]*nums[i] <= nums[p]*nums[p]) {
                arr2[arr2.length-counter] = nums[p]*nums[p];
                counter++;
                p--;
            } else if (nums[i]*nums[i] > nums[p]*nums[p]) {
                arr2[arr2.length-counter] = nums[i]*nums[i];
                counter++;
                i++;
            }
        }
        return arr2;
    }
}
